/**
 * 百人场牛牛
 * @author 袁安
 *
 */
class P100GameSceneData
{

    private static _instance: P100GameSceneData;
    public static getInstance(): P100GameSceneData 
    {
        if (!this._instance) {
            this._instance = new P100GameSceneData();
        }
        return this._instance;
    }

    private RECORD_NUMS: number = 8;
    private COLOR_NUMS: number = 4;
    private selfUserData: base.UserData;            // 自己的数据
    private historyDataArray: any[] = [];                // 历史数据
    private gameInfo: any;                          // 游戏的基本信息
    private gameEndRes: any;                        // 游戏结束结构
    private totalBetValueArray: number[] = [0, 0, 0, 0];                  // 每个下注区当前下注总值的具体数值
    private selfBetValueArray: number[] = [0, 0, 0, 0];                   // 每个下注区域自己的当前下注总值
    private bankerReqArray: number[] = [];// 请求上庄的列表
    private currentBanker: number = -1;
    private maxJettonLimit: number = 0; // 最大下注上限
    private bankerScore: number = 0;    // 庄家当前金币数
    public constructor()
    {
    }

    // 初始化游戏数据
    public initData()
    {
        this.gameEndRes = [];
        this.totalBetValueArray = [0, 0, 0, 0];
        this.selfBetValueArray = [0, 0, 0, 0];
        this.bankerScore = 0;
    }

    // 设置游戏基本信息
    public setGameInfo(data: any): void
    {
        this.gameInfo = data;
    }
    public getGameInfo(): any
    {
        return this.gameInfo;
    }

    // 设置下注的一些限制
    public setJettonLimit(data: any): void
    {
        this.maxJettonLimit = data.lUserMaxScore;
        if(data.wBankerUser == 65535)
        {
            // 如果是系统坐庄，则将庄家的钱设置为 888888888
            this.bankerScore = 888888888;
        }
        else
        {
            this.bankerScore = data.lBankerScore;
        }
        
    }
    public getMaxJettonLimit(): number
    {
        return this.maxJettonLimit;
    }
    public getBankerScore(): number
    {
        return this.bankerScore;
    }

    // 设置游戏记录
    public setGameRecord(data: any): void
    {
        for (var i: number = data.gameRecord.length - 1; i > 0; --i)
        {
            this.historyDataArray.push(data.gameRecord[i]);
            if (this.historyDataArray.length >= this.RECORD_NUMS)
            {
                break;
            }
        }
        this.historyDataArray = this.historyDataArray.reverse();
    }

    public setCurrentBanker(data: any): void
    {
        this.currentBanker = data;
    }

    public getCurrentBanker(): number
    {
        return this.currentBanker;
    }

    public getCurrentBankerData(): base.UserData
    {
        var bankerData: base.UserData = null;
        if (this.currentBanker == 65535)
        {
            bankerData = new base.UserData();
            bankerData.nickName = "aaaaaaaaa";
            // bankerData.faceID = 1;
            bankerData.UserScoreInfo = {};
            bankerData.UserScoreInfo.lScore = 8888888;
        }
        else
        {
            // bankerData = base.Utils.getItem(this.playerList, "chairID", this.currentBanker);
        }
        return bankerData;
    }

    // 设置游戏结束信息
    public setGameEndRes(data: any): void
    {
        this.gameEndRes = data;
        this.historyDataArray.shift()
        this.historyDataArray.push(data.gameRecord);
        AppData.getInstance().userData.money += data.lUserReturnScore;
    }
    public getGameEndRes(): any
    {
        return this.gameEndRes;
    }

    // 设置各个下注区域的当前下注值
    public setJettonInfo(data: any): void
    {
        this.totalBetValueArray[data.cbJettonArea - 1] += data.lJettonScore;
        if (data.wChairID == AppData.getInstance().userData.chairID)
        {
            this.selfBetValueArray[data.cbJettonArea - 1] += data.lJettonScore;
            AppData.getInstance().userData.money -= data.lJettonScore;
        }
    }

    // 获得下注区域总下注值的数组
    public getTotalBetValueArray(): any[]
    {
        return this.totalBetValueArray;
    }
    // 获得自己下注区的总下注值的数组
    public getSelfBetValueArray(): any[]
    {
        return this.selfBetValueArray;
    }
    // 获得自己的总下注值
    public getSelfBetTotalValue(): number
    {
        var totalValue: number = 0;
        this.selfBetValueArray.forEach(value => totalValue += value);
        return totalValue;
    }

    // 获得在线玩家的基本信息
    public getOnlinePlayerList(): eui.ArrayCollection
    {
        // var playerList: base.PlayerData[] = this.getPlayersByTableID()
        var showInfoArray: any[] = [];
        // for (var i: number = 0; i < playerList.length; ++i)
        // {
        //     var playerData: base.PlayerData = playerList[i];
        //     var showInfo: any = {};
        //     showInfo.imgHead = "";//DataConfig.headID[playerData.faceID].image
        //     showInfo.userName = playerData.nickName;
        //     showInfo.userScore = playerData.UserScoreInfo.lScore;
        //     showInfoArray.push(showInfo);
        // }
        return new eui.ArrayCollection(showInfoArray);
    }

    // 获得历史记录
    public getTrendList(): eui.ArrayCollection
    {
        var trendList: any[] = [];
        for (var i: number = 0; i < this.historyDataArray.length; ++i)
        {
            var record: any[] = this.historyDataArray[i];
            if (record.length <= this.COLOR_NUMS)
            {
                var recordInfo: any =
                    {
                        imgValue0: (record[0] ? "p100Trendwin_png" : "p100Trendlose_png"),
                        imgValue1: (record[1] ? "p100Trendwin_png" : "p100Trendlose_png"),
                        imgValue2: (record[2] ? "p100Trendwin_png" : "p100Trendlose_png"),
                        imgValue3: (record[3] ? "p100Trendwin_png" : "p100Trendlose_png"),
                    };
                trendList.push(recordInfo);
            }
        }
        return new eui.ArrayCollection(trendList);
    }

    // 等待上庄列表
    public addReqBankerUser(data: any): void
    {
        var reqBanker: number = data.wApplyUser;
        this.bankerReqArray.push(reqBanker);
    }

    // 用户取消上庄
    public removeReqBankerUser(data: any): void
    {
        var reqBanker: number = data.wCancelUser;
        this.bankerReqArray.splice(this.bankerReqArray.indexOf(reqBanker));
    }

    public getBankerReqList(): eui.ArrayCollection
    {
        var bankerReqList: any[] = [];
        // for (var i: number = 0; i < this.bankerReqArray.length; ++i)
        // {
        //     var nSeatID: number = this.bankerReqArray[i];
        //     var playerData: base.PlayerData = base.Utils.getItem(this.playerList, "seatID", nSeatID);
        //     var bankerReqInfo: any = {};
        //     if (playerData)
        //     {
        //         bankerReqInfo.nickname = playerData.nickName;
        //         bankerReqInfo.scorenums = base.Utils.formatCurrency(playerData.UserScoreInfo.lScore);
        //         bankerReqList.push(bankerReqInfo);
        //     }
        // }
        return new eui.ArrayCollection(bankerReqList);
    }
}
